package com.kaer.plugin.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class IOUtils {
	private IOUtils(){};
	
	/**
	 * 将输入流中的数据全部读取出来, 一次性返回
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static byte[] load(InputStream is) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len;
		while ((len = is.read(buffer)) != -1)
			baos.write(buffer, 0, len);
		baos.close();
		is.close();
		return baos.toByteArray();
	}

	/**
	 * 根据文件得到字节流
	 * 
	 * @param file
	 * @return
	 */
	public static byte[] getFileByte(File file) {
		if (!file.exists()) {
			return null;
		}
		try {
			FileInputStream fis = new FileInputStream(file);
			int len = fis.available();
			byte[] bytes = new byte[len];
			fis.read(bytes);
			fis.close();
			return bytes;
		} catch (Exception e) {

		}

		return null;
	}

}
